package com.xteam.youin_supplier.fragment;


import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.PrivateInfo;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 个人信息设置
 */
@EFragment(R.layout.fragment_private_info)
public class PrivateInfoFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.saveBtn)
    TextView saveBtn;

    @ViewById(R.id.private_name_content)
    EditText name;
    @ViewById(R.id.private_connect_content)
    EditText connect;
    @ViewById(R.id.private_addr_content)
    EditText addr;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.private_info_title));
        saveBtn.setVisibility(View.VISIBLE);

        loadData();
    }

    private void loadData() {
        requestParams = "supplier_id="+ GlobalData.userInfo.getData().getSupplier_id();
        requestUrl = RequestAddr.prvateSetShow + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    PrivateInfo privateInfo;
    private void dealData(Object object) {
        try {
            privateInfo = gson.fromJson(object.toString(), PrivateInfo.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            MainActivity.getInstance().getSupportFragmentManager().popBackStack();
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

        name.setText(privateInfo.getData().getSupplier_name());
        addr.setText(privateInfo.getData().getAddress());
        connect.setText(privateInfo.getData().getMobile());
    }

    @Click({R.id.back_btn, R.id.saveBtn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.saveBtn:
                if (checkValidate()) {
                    saveInfo();
                }
                break;
        }
    }

    private boolean checkValidate() {
        return true;
    }

    private void saveInfo() {
        requestParams = "address="+addr.getText().toString().trim()
        +"&mobile="+connect.getText().toString().trim()
                +"&supplier_id="+GlobalData.userInfo.getData().getSupplier_id()
                +"&supplier_name="+name.getText().toString().trim();
        requestUrl = RequestAddr.prvateEdit+requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }
}
